package com.bifrii.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import com.bifrii.transferObjects.Event;
import com.bifrii.transferObjects.User;
import com.bifrii.dao.UIDGenerator;

public class JDBCPendingEventDAO implements IPendingEventDAO
{
	@Override
	public List<Event> selectAllPendingEvents(Integer userId)
	{
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        List<Event> pendingEvents = new LinkedList<Event>();
        ResultSet result = null;

        try
        {
            String sql = "select e.oid, e.idEvent, e.idCommunity, e.idCreator, e.name, e.description, e.avatar, e.limitAssistance from PendingEvent pe, Event e where pe.idUser = ? and pe.idEvent = e.idEvent order by e.idEvent desc";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next())
            {
                Event event = new Event();
                event.setOid(result.getString("oid"));
                event.setIdEvent(result.getInt("idEvent"));
                event.setIdCommunity(result.getInt("idCommunity"));
                event.setIdCreator(result.getInt("idCreator"));
                event.setName(result.getString("name"));
                event.setDescription(result.getString("description"));
                event.setAvatar(result.getString("avatar"));
                event.setLimitAssistance(result.getInt("limitAssistance"));
             
                pendingEvents.add(event);
            }   
        }
        catch (SQLException ex)
        {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } 
        finally 
        {
            ConnectionManager.getInstance().checkIn(conn);
            
            try 
            {
                if (result != null) result.close();
                if (stmt != null) stmt.close();
            }
            catch (SQLException ex)
            {
            }
        }
        
        return pendingEvents;
	}

	@Override
	public void insert(Event event, User user)
	{
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "insert into PendingEvent (oid, idEvent, idUser) values (?, ?, ?);";
        PreparedStatement stmt = null;

        try
        {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, UIDGenerator.getInstance().getKey());
            stmt.setInt(2, event.getIdEvent());
			stmt.setInt(3, user.getIdUser());
            stmt.executeUpdate();

        }
        catch (SQLException ex)
        {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        }
        finally
        {
            try 
            {
                if (stmt != null) stmt.close();
            }
            catch (SQLException ex) 
            {
            }
        }
	}

	@Override
	public void delete(Integer eventId, Integer userId)
	{
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "delete from PendingEvent where (idEvent = ? and idUser = ?)";
        PreparedStatement stmt = null;

        try 
        {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, eventId);
            stmt.setInt(2, userId);
            stmt.executeUpdate();
        }
        catch (SQLException e)
        {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        }
        finally
        {
            try
            {
                if (stmt != null) stmt.close();
            }
			catch (SQLException e) 
            {
            }
        }
	}
}
